﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Security;
using System.Threading;

namespace TeknoromaWebService
{
    /// <summary>
    /// Summary description for TeknoromaWebServiceClass
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class TeknoromaWebServiceClass : System.Web.Services.WebService
    {

        public KimlikBilgisi kb;
        public bool KimlikDogrulama()
        {
            using (DBBaglantiDataContext db = new DBBaglantiDataContext())
            {
                var sorgu = (from x in db.WebServiceSettings where x.WebServiceSettingUser == kb.User && x.WebServiceSettingPassword == kb.Password && x.WebServiceSettingSecurtyCode == kb.SecurtyCode select x).FirstOrDefault();
                if (sorgu != null)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }

        [WebMethod]
        [SoapHeader("kb")]
        public string LoginValidation(string UserName, string Password)
        {
            if (KimlikDogrulama())
            {
                if (Membership.ValidateUser(UserName, Password))
                {
                    return Membership.GetUser(UserName).ProviderUserKey.ToString();
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        [WebMethod]
        [SoapHeader("kb")]
        public string[] GetUserRoles(string UserName)
        {
            if (KimlikDogrulama())
            {
                string[] RoleList = Roles.GetRolesForUser(UserName);
                Thread.Sleep(5000);
                return RoleList;
            }
            else
            {
                string[] RoleList = new string[0];
                return RoleList;
            }
        }

        [WebMethod]
        [SoapHeader("kb")]
        public void KullaniciOlustur(string _username, string _password,string _email, string _passwordQuestion, string _passwordAnswer,bool _isApproved, out MembershipCreateStatus durum)
        {
            
            Membership.CreateUser(_username, _password, _email, _passwordQuestion, _passwordAnswer, _isApproved,out durum);
        }
         

    }

    public class KimlikBilgisi : SoapHeader
    {
        public string User { get; set; }
        public string Password { get; set; }
        public string SecurtyCode { get; set; }
    }
}
